
#ifndef xsdp_hpp
#define xsdp_hpp

#include <stdio.h>
#include <glib.h>

#include "codecs.hpp"
#include "rtp/rtp_exten.hpp"

typedef struct xsdp_rtpmap{
    const char * name;
    int payload_type;
    int freq;
    int channels;
}xsdp_rtpmap;

enum class xsdp_fmtp_type {
    apt,
    minptime,
    useinbandfec,
    levelAsymmetryAllowed,
    packetizationMode,
    profileLevelId,
};

typedef struct xsdp_fmtp {
    xsdp_fmtp_type type;
    int pt;
    union {
        int apt;
        int minptime;
        int useinbandfec;
        int levelAsymmetryAllowed;
        int packetizationMode;
        char* profileLevelId;
    };
}xsdp_fmtp;

enum class xsdp_sendrecv_type {
    sendrecv,
    sendonly,
    recvonly,
};

typedef struct {
    int         id;
    const char* uri;
} xsdp_rtp_exten;

#define XSDP_MAX_SSRCS 4
typedef struct xsdp_stream{
    const char * stream_name;
//    int stream_id;
    const char * mid;
    int mlineindex;
    int is_rtcpmux;
    const char * ice_ufrag;
    const char * ice_pwd;
    const char * fingerprint_hash_name;
    const char * fingerprint_str;
    unsigned int ssrcs[XSDP_MAX_SSRCS];
    int ssrc_count;
    RtpExtensionMap* rtp_exten_map;
    // todo: remove payloads use codecs instead
//    GSList * payloads;
    GSList * str_candidates;
    GSList * rtpmaps;
    GSList * fmtps;
    xsdp_sendrecv_type sendrecv;
    bool transport_cc_enabled;
    bool abs_send_time_enabled;
}xsdp_stream;

typedef struct xsdpinfo{
    char * sdpbuf;
    char * sdpbuf_orig;
    int sdp_len;
    int is_bundle;
    char * sdp_bundles;
    int sdp_bundles_len;
    int stream_count;
    xsdp_stream * audio_stream;
    xsdp_stream * video_stream;
    xsdp_stream  audio_stream_storage;
    xsdp_stream  video_stream_storage;
    rtc_codecs_t* codecs;
}xsdpinfo;

int xsdp_parse(const char * sdpdesc, int sdp_len, xsdpinfo **pinfo);
void xsdp_free_info(xsdpinfo * info);
void xsdp_print(xsdpinfo *info, const char * prefix);
void xsdp_stream_print(xsdp_stream * stream, const char * prefix);
void xsdp_stream_free(xsdp_stream * stream);
size_t xsdp_format_audio_sdp(char *buf, size_t len, RtpExtensionMap* extens, rtc_codecs_t *codecs, const char *mid, const char *setup,
                             const char *usr, const char *pwd, const char *key, const char *sendrecv, bool mux_rtcp,
                             uint32_t ssrc, const char *cname, const char *stream, const char *track);

size_t xsdp_format_video_sdp(char *buf, size_t len, RtpExtensionMap* extens, rtc_codecs_t *codecs, bool enable_fec, int bitrate_max,
                             int bitrate_min, const char *mid, const char *setup, const char *usr, const char *pwd,
                             const char *key, const char *sendrecv, bool mux_rtcp, uint32_t ssrc_pri, uint32_t ssrc_rtx,
                             const char *cname, const char *stream, const char *track);

#endif /* xsdp_hpp */
